package h0827;

import java.util.Arrays;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 颜色分类
 * @date 2024/8/27 21:11
 */
public class SortColor {
    public static void main(String[] args) {
        int[] nums = {0, 1, 2, 0, 2, 1};
       getRes(nums);
        System.out.println(Arrays.toString(nums));
    }
    private static void getRes(int[] nums){
       int low=0;
       int mid=0;
       int high= nums.length-1;
        while (mid<high){
            if (nums[mid]==0){
                swap(nums,mid,low);
                low++;
                mid++;
            }else if (nums[mid]==2){
                swap(nums,mid,high);
                high--;
            }else {
                mid++;//等于1
            }
        }
    }

    private static void swap(int[] nums, int mid, int low) {
        int temp=nums[mid];
        nums[mid]=nums[low];
        nums[low]=temp;
    }
}
